<!DOCTYPE html>



  


<html class="theme-next mist use-motion" lang="zh-CN">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.3" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.3">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.3">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.3">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.3" color="#222">





  <meta name="keywords" content="Hexo, NexT" />





  <link rel="alternate" href="/atom.xml" title="Likianta Blog" type="application/atom+xml" />






<meta name="description" content="1. 首先看一下最终的效果图 本章我们只制作左分页，这是一个以卡片为单位组成的可滚动的列表，我们需要用到CardView（卡片布局）和RecyclerView（列表布局）。">
<meta property="og:type" content="article">
<meta property="og:title" content="03 Anykey左分页布局RecyclerView">
<meta property="og:url" content="https://likianta.coding.me/2017/1123205552/index.html">
<meta property="og:site_name" content="Likianta Blog">
<meta property="og:description" content="1. 首先看一下最终的效果图 本章我们只制作左分页，这是一个以卡片为单位组成的可滚动的列表，我们需要用到CardView（卡片布局）和RecyclerView（列表布局）。">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="http://ozurciydg.bkt.clouddn.com/17-11-23/12576326.jpg">
<meta property="og:image" content="http://ozurciydg.bkt.clouddn.com/17-11-23/5028616.jpg">
<meta property="og:image" content="http://ozurciydg.bkt.clouddn.com/17-11-23/15192474.jpg">
<meta property="og:updated_time" content="2017-12-31T15:25:54.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="03 Anykey左分页布局RecyclerView">
<meta name="twitter:description" content="1. 首先看一下最终的效果图 本章我们只制作左分页，这是一个以卡片为单位组成的可滚动的列表，我们需要用到CardView（卡片布局）和RecyclerView（列表布局）。">
<meta name="twitter:image" content="http://ozurciydg.bkt.clouddn.com/17-11-23/12576326.jpg">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Mist',
    version: '5.1.3',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://likianta.coding.me/2017/1123205552/"/>





  <title>03 Anykey左分页布局RecyclerView | Likianta Blog</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Likianta Blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tutorials">
          <a href="/tags/tutorial-catalog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            教程
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://likianta.coding.me/2017/1123205552/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Likianta">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="http://ozurciydg.bkt.clouddn.com/18-2-12/72826549.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Likianta Blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">03 Anykey左分页布局RecyclerView</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-11-23T20:55:52+08:00">
                2017-11-23
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/passport-pandora/" itemprop="url" rel="index">
                    <span itemprop="name">从零开发Anykey</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  2,998
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  13
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1 id="1-首先看一下最终的效果图"><a href="#1-首先看一下最终的效果图" class="headerlink" title="1. 首先看一下最终的效果图"></a>1. 首先看一下最终的效果图</h1><p><img src="http://ozurciydg.bkt.clouddn.com/17-11-23/12576326.jpg" alt="page1"></p>
<p>本章我们只制作左分页，这是一个以卡片为单位组成的可滚动的列表，我们需要用到CardView（卡片布局）和RecyclerView（列表布局）。</p>
<a id="more"></a>
<h1 id="2-配置RecyclerView和CardView"><a href="#2-配置RecyclerView和CardView" class="headerlink" title="2. 配置RecyclerView和CardView"></a>2. 配置RecyclerView和CardView</h1><p>RecyclerView和CardView需要预先在app/build.gradle中配置，代码如下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">dependencies &#123;</span><br><span class="line">    ...</span><br><span class="line">    compile <span class="string">'com.android.support:appcompat-v7:25.3.1'</span></span><br><span class="line">    </span><br><span class="line">    compile <span class="string">'com.android.support:recyclerview-v7:25.3.1'</span> <span class="comment">//配置recyclerview，版本与上面的appcompat保持一致，这里我的就是v7:25.3.1</span></span><br><span class="line">    compile <span class="string">'com.android.support:cardview-v7:25.3.1'</span> <span class="comment">//配置cardview</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>写好了以后，注意在该页的右上角点击“Sync Now”同步一下：</p>
<p><img src="http://ozurciydg.bkt.clouddn.com/17-11-23/5028616.jpg" alt="Sync Now!"></p>
<h1 id="3-制作RecyclerView布局"><a href="#3-制作RecyclerView布局" class="headerlink" title="3. 制作RecyclerView布局"></a>3. 制作RecyclerView布局</h1><p>在<code>page_item_all.xml</code>布局中写RecyclerView布局。这个和上章中的代码差不多是一样的，基本没什么变化，只是将背景色改了一下：</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">android.support.v7.widget.RecyclerView</span> <span class="attr">xmlns:android</span>=<span class="string">"http://schemas.android.com/apk/res/android"</span></span></span><br><span class="line"><span class="tag">    <span class="attr">xmlns:tools</span>=<span class="string">"http://schemas.android.com/tools"</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:id</span>=<span class="string">"@+id/recyclerView"</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:layout_width</span>=<span class="string">"match_parent"</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:layout_height</span>=<span class="string">"wrap_content"</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:background</span>=<span class="string">"@color/mycolorBackground"</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--背景色改成了浅灰色--&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">android.support.v7.widget.RecyclerView</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>这就是整体的代码，看起来很简洁。<br>background的值是在<code>res/values/colors.xml</code>中定义的：</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">resources</span>&gt;</span></span><br><span class="line">    ...</span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!--自制色卡--&gt;</span></span><br><span class="line">    ...</span><br><span class="line">    <span class="tag">&lt;<span class="name">color</span> <span class="attr">name</span>=<span class="string">"mycolorBackground"</span>&gt;</span>#dedede<span class="tag">&lt;/<span class="name">color</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">resources</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>接下来我们要往这个布局中动态地生成小卡片，所以我们要制作卡片的模板。</p>
<h1 id="4-新建card-xml制作卡片模板"><a href="#4-新建card-xml制作卡片模板" class="headerlink" title="4. 新建card.xml制作卡片模板"></a>4. 新建<code>card.xml</code>制作卡片模板</h1><p>在卡片中包含以下属性：</p>
<ol>
<li>头像（头像默认是由标题的拼音的首字母生成的）</li>
<li>标题</li>
<li>摘要（摘要信息=帐号+密码+备注）</li>
<li>功能按钮：<ol>
<li>弹出菜单</li>
<li>复制帐号到剪切板</li>
<li>复制密码到剪切板</li>
</ol>
</li>
</ol>
<p>代码如下：</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">android.support.v7.widget.CardView</span> <span class="attr">xmlns:android</span>=<span class="string">"http://schemas.android.com/apk/res/android"</span></span></span><br><span class="line"><span class="tag">    <span class="attr">xmlns:app</span>=<span class="string">"http://schemas.android.com/apk/res-auto"</span></span></span><br><span class="line"><span class="tag">    <span class="attr">xmlns:tools</span>=<span class="string">"http://schemas.android.com/tools"</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:layout_width</span>=<span class="string">"match_parent"</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:layout_height</span>=<span class="string">"120dp"</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:layout_margin</span>=<span class="string">"5dp"</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:elevation</span>=<span class="string">"4dp"</span></span></span><br><span class="line"><span class="tag">    <span class="attr">app:cardCornerRadius</span>=<span class="string">"8dp"</span></span></span><br><span class="line"><span class="tag">    &gt;</span></span><br><span class="line">    <span class="comment">&lt;!--elevation表示卡片的高度，cardCornerRadius表示卡片四个角的弧度，xmlns:tools用于识别TextView中的\n换行符--&gt;</span></span><br><span class="line">    </span><br><span class="line">    <span class="comment">&lt;!--CardView本身是一个FrameLayout，显然不适合摆放控件。因此为了充分利用空间，要内建一个RelativeLayout来盛放所有子控件--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">RelativeLayout</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:layout_width</span>=<span class="string">"match_parent"</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:layout_height</span>=<span class="string">"match_parent"</span>&gt;</span></span><br><span class="line">        </span><br><span class="line">        <span class="comment">&lt;!--首先思路就是把RelativeLayout分为左中右三个部分，左边贴一个头像；中间占的面积最大，用来写标题和摘要；右边则放置功能按钮--&gt;</span></span><br><span class="line">        </span><br><span class="line">        <span class="comment">&lt;!--现在写的是中间的布局LinearLayout。在LinearLayout中上半部分显示标题，下半部分显示摘要--&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">LinearLayout</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:layout_width</span>=<span class="string">"match_parent"</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:layout_height</span>=<span class="string">"match_parent"</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:layout_marginLeft</span>=<span class="string">"80dp"</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:layout_marginRight</span>=<span class="string">"40dp"</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:orientation</span>=<span class="string">"vertical"</span>&gt;</span></span><br><span class="line">            <span class="comment">&lt;!--注意设定LinearLayout的方向为vertical--&gt;</span></span><br><span class="line">            </span><br><span class="line">            <span class="comment">&lt;!--标题，深色，字号较大，高度比重为40%--&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">TextView</span></span></span><br><span class="line"><span class="tag">                <span class="attr">android:id</span>=<span class="string">"@+id/cardTitle"</span></span></span><br><span class="line"><span class="tag">                <span class="attr">android:layout_width</span>=<span class="string">"match_parent"</span></span></span><br><span class="line"><span class="tag">                <span class="attr">android:layout_height</span>=<span class="string">"0dp"</span></span></span><br><span class="line"><span class="tag">                <span class="attr">android:layout_weight</span>=<span class="string">"4"</span></span></span><br><span class="line"><span class="tag">                <span class="attr">android:gravity</span>=<span class="string">"bottom"</span></span></span><br><span class="line"><span class="tag">                <span class="attr">android:padding</span>=<span class="string">"4dp"</span></span></span><br><span class="line"><span class="tag">                <span class="attr">android:text</span>=<span class="string">"TitleTest"</span></span></span><br><span class="line"><span class="tag">                <span class="attr">android:textSize</span>=<span class="string">"32sp"</span></span></span><br><span class="line"><span class="tag">                <span class="attr">android:textColor</span>=<span class="string">"@color/mycolorText1"</span>/&gt;</span></span><br><span class="line">    </span><br><span class="line">            <span class="comment">&lt;!--摘要，浅色或同色，字号比正常文字还要小，高度比重为60%，限制显示三行文字--&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">TextView</span></span></span><br><span class="line"><span class="tag">                <span class="attr">android:id</span>=<span class="string">"@+id/cardSummary"</span></span></span><br><span class="line"><span class="tag">                <span class="attr">android:layout_width</span>=<span class="string">"match_parent"</span></span></span><br><span class="line"><span class="tag">                <span class="attr">android:layout_height</span>=<span class="string">"0dp"</span></span></span><br><span class="line"><span class="tag">                <span class="attr">android:layout_weight</span>=<span class="string">"6"</span></span></span><br><span class="line"><span class="tag">                <span class="attr">android:paddingTop</span>=<span class="string">"3dp"</span></span></span><br><span class="line"><span class="tag">                <span class="attr">android:maxLines</span>=<span class="string">"3"</span></span></span><br><span class="line"><span class="tag">                <span class="attr">android:lines</span>=<span class="string">"3"</span></span></span><br><span class="line"><span class="tag">                <span class="attr">android:ellipsize</span>=<span class="string">"end"</span></span></span><br><span class="line"><span class="tag">                <span class="attr">android:text</span>=<span class="string">"name: ______\npassword: ______\nnote: this view limited 3 lines"</span> /&gt;</span></span><br><span class="line">            <span class="comment">&lt;!--maxLines表示最大行数，lines表示即使只有一行字也要占用三行字的高度空间，ellipsize表示多出的字数表示为省略号</span></span><br><span class="line"><span class="comment">            参考：http://blog.csdn.net/qq_31403303/article/details/51506524--&gt;</span></span><br><span class="line">            </span><br><span class="line">        <span class="tag">&lt;/<span class="name">LinearLayout</span>&gt;</span></span><br><span class="line">    </span><br><span class="line">        <span class="comment">&lt;!--接下来写的是右边的布局，也是LinearLayout布局。从上到下依次显示三个按钮：菜单、复制name、复制password--&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">LinearLayout</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:layout_width</span>=<span class="string">"40dp"</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:layout_height</span>=<span class="string">"match_parent"</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:layout_alignParentRight</span>=<span class="string">"true"</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:layout_margin</span>=<span class="string">"5dp"</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:orientation</span>=<span class="string">"vertical"</span>&gt;</span></span><br><span class="line">    </span><br><span class="line">            <span class="comment">&lt;!--菜单按钮--&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">ImageButton</span></span></span><br><span class="line"><span class="tag">                <span class="attr">android:id</span>=<span class="string">"@+id/cardMenu"</span></span></span><br><span class="line"><span class="tag">                <span class="attr">android:layout_width</span>=<span class="string">"match_parent"</span></span></span><br><span class="line"><span class="tag">                <span class="attr">android:layout_height</span>=<span class="string">"40dp"</span> /&gt;</span></span><br><span class="line">    </span><br><span class="line">            <span class="comment">&lt;!--两个复制按钮平分剩下的高度空间--&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">ImageButton</span></span></span><br><span class="line"><span class="tag">                <span class="attr">android:id</span>=<span class="string">"@+id/copyName"</span></span></span><br><span class="line"><span class="tag">                <span class="attr">android:layout_gravity</span>=<span class="string">"center"</span></span></span><br><span class="line"><span class="tag">                <span class="attr">android:layout_width</span>=<span class="string">"20dp"</span></span></span><br><span class="line"><span class="tag">                <span class="attr">android:layout_height</span>=<span class="string">"0dp"</span></span></span><br><span class="line"><span class="tag">                <span class="attr">android:layout_weight</span>=<span class="string">"1"</span> /&gt;</span></span><br><span class="line">            </span><br><span class="line">            <span class="tag">&lt;<span class="name">ImageButton</span></span></span><br><span class="line"><span class="tag">                <span class="attr">android:id</span>=<span class="string">"@+id/copyPassword"</span></span></span><br><span class="line"><span class="tag">                <span class="attr">android:layout_gravity</span>=<span class="string">"center"</span></span></span><br><span class="line"><span class="tag">                <span class="attr">android:layout_width</span>=<span class="string">"20dp"</span></span></span><br><span class="line"><span class="tag">                <span class="attr">android:layout_height</span>=<span class="string">"0dp"</span></span></span><br><span class="line"><span class="tag">                <span class="attr">android:layout_weight</span>=<span class="string">"1"</span> /&gt;</span></span><br><span class="line">        </span><br><span class="line">        <span class="tag">&lt;/<span class="name">LinearLayout</span>&gt;</span></span><br><span class="line">    </span><br><span class="line">        <span class="comment">&lt;!--不要忘了左边部分。之所以留待最后才写，是为了让头像以及分割线最后加载，这样就处于其它二者的上方了--&gt;</span></span><br><span class="line">        <span class="comment">&lt;!--先做分割线，非常细，非均等分割（上窄下宽）--&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">TextView</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:layout_width</span>=<span class="string">"match_parent"</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:layout_height</span>=<span class="string">"0.4dp"</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:layout_marginTop</span>=<span class="string">"46dp"</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:background</span>=<span class="string">"#000000"</span> /&gt;</span></span><br><span class="line">    </span><br><span class="line">        <span class="comment">&lt;!--头像--&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">ImageView</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:id</span>=<span class="string">"@+id/cardHead"</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:layout_width</span>=<span class="string">"50dp"</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:layout_height</span>=<span class="string">"50dp"</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:layout_marginLeft</span>=<span class="string">"10dp"</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:layout_marginTop</span>=<span class="string">"20dp"</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:src</span>=<span class="string">"@color/mycolorText2"</span> /&gt;</span></span><br><span class="line">    </span><br><span class="line">    <span class="tag">&lt;/<span class="name">RelativeLayout</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">android.support.v7.widget.CardView</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>卡片预览图如下：</p>
<p><img src="http://ozurciydg.bkt.clouddn.com/17-11-23/15192474.jpg" alt="CardView"></p>
<p>至此两个布局文件都写好了。下面才是本章的重点，为了方便理解，我们分为“卡片的属性及适配”和“列表的加载”两大部分来讲，其中适配器和模板属性的建立是所有业务处理的基础，也是本章的难点。</p>
<h1 id="5-卡片的属性及适配"><a href="#5-卡片的属性及适配" class="headerlink" title="5. 卡片的属性及适配"></a>5. 卡片的属性及适配</h1><h2 id="5-1-首先新建Card-java，制作卡牌模型"><a href="#5-1-首先新建Card-java，制作卡牌模型" class="headerlink" title="5.1. 首先新建Card.java，制作卡牌模型"></a>5.1. 首先新建Card.java，制作卡牌模型</h2><p>制作卡片的主要思路：</p>
<ol>
<li>卡片有哪些属性要传入数据：头像id（本章暂时先不做），标题（cardTitle）以及摘要（cardSummary）</li>
<li>生成传入和传出接口（getter＆setter）</li>
</ol>
<p>代码如下：<br>（注：头像id、三个按钮都先不做）</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Card</span> </span>&#123;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">//定义一个卡片的属性</span></span><br><span class="line">    <span class="keyword">private</span> String cardTitle; <span class="comment">//卡片标题</span></span><br><span class="line">    <span class="keyword">private</span> String cardSummary; <span class="comment">//卡片的摘要内容（摘要=用户名+密码+备注）</span></span><br><span class="line">    <span class="comment">// 摘要是后面学习的一个重点，在完成一个卡片的填写后，这个卡片的摘要会自动生成。卡片摘要也是软件搜索功能的重要依据</span></span><br><span class="line">    <span class="comment">//private ImageView cardHead; //头像</span></span><br><span class="line">    <span class="comment">//private ImageButton cardMenu; //卡片菜单按钮</span></span><br><span class="line">    <span class="comment">//private ImageButton copyName; //复制按钮，复制用户名</span></span><br><span class="line">    <span class="comment">//private ImageButton copyPassword; //复制按钮，复制密码</span></span><br><span class="line">    </span><br><span class="line">    <span class="comment">//一张卡片完整的属性为：</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">Card</span><span class="params">(String cardTitle, String cardSummary)</span> </span>&#123;</span><br><span class="line">        <span class="comment">//将外界传入的3个参数赋值给Card内部类</span></span><br><span class="line">        <span class="comment">//this.cardHead = cardHead;</span></span><br><span class="line">        <span class="keyword">this</span>.cardTitle = cardTitle;</span><br><span class="line">        <span class="keyword">this</span>.cardSummary = cardSummary;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">//创建卡片的Getter&amp;Setter方法</span></span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">getCardSummary</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> cardSummary;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setCardSummary</span><span class="params">(String cardSummary)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.cardSummary = cardSummary;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">getCardTitle</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> cardTitle;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setCardTitle</span><span class="params">(String cardTitle)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.cardTitle = cardTitle;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>getter和setter的快捷键：</p>
<ol>
<li>写好<code>private String cardTitle</code>和<code>private String cardSummary</code>后点击鼠标右键</li>
<li>右键列表 - Generate - Getter and Setter - 弹出对话框 - 按住Ctrl选择cardTitle和cardSummary - OK</li>
<li>自动生成这两个变量的getter和setter方法</li>
</ol>
<h2 id="5-2-然后制作卡片适配器CardAdapter"><a href="#5-2-然后制作卡片适配器CardAdapter" class="headerlink" title="5.2. 然后制作卡片适配器CardAdapter"></a>5.2. 然后制作卡片适配器CardAdapter</h2><p>适配器是连接数据（比如说json数组）和模板（比如这里的Card）之间的桥梁，用一个不太准确的说法来说，它的作用就是把数组切片并把每一片贴到一张张白卡上面，然后RecyclerView利用setAdapter()方法就能很轻松的载入这些写好的卡片了。</p>
<p>新建CardAdapter.java，代码如下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">CardAdapter</span> <span class="keyword">extends</span> <span class="title">RecyclerView</span>.<span class="title">Adapter</span>&lt;<span class="title">CardAdapter</span>.<span class="title">ViewHolder</span>&gt; </span>&#123;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">/* 什么是适配器（Adapter）？</span></span><br><span class="line"><span class="comment">    适配器为原料做适配工作。</span></span><br><span class="line"><span class="comment">    用一种形象的说法，比如把适配器看作一座工厂，适配器给卡片们组配上了手臂（ViewHolder），更方便卡片去抓握数据了，这就是适配的好处。</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    </span><br><span class="line">    <span class="keyword">private</span> Context context; <span class="comment">//定义一个context，由于没有赋值，所以现在是null的状态</span></span><br><span class="line">    <span class="keyword">private</span> List&lt;Card&gt; cardList;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">CardAdapter</span><span class="params">(List&lt;Card&gt; cardList)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.cardList = cardList;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> ViewHolder <span class="title">onCreateViewHolder</span><span class="params">(ViewGroup parent, <span class="keyword">int</span> viewType)</span> </span>&#123;</span><br><span class="line">        <span class="comment">//首先传入context</span></span><br><span class="line">        <span class="keyword">if</span> (context == <span class="keyword">null</span>) &#123;</span><br><span class="line">            context = parent.getContext(); <span class="comment">//这里的parent就是指列表页(RecyclerView)所属的类了</span></span><br><span class="line">        &#125;</span><br><span class="line">        </span><br><span class="line">        View card = LayoutInflater.from(context).inflate(R.layout.card, parent, <span class="keyword">false</span>);</span><br><span class="line">        <span class="comment">//inflate的第三个参数表示是否连接该布局和父控件。由于系统已经插入了这个布局到父控件，所以设为false（若写true则会产生一个多余的parent）</span></span><br><span class="line">        </span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> ViewHolder(card); <span class="comment">//获得了一张白卡</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">static</span> <span class="class"><span class="keyword">class</span> <span class="title">ViewHolder</span> <span class="keyword">extends</span> <span class="title">RecyclerView</span>.<span class="title">ViewHolder</span> </span>&#123;</span><br><span class="line">        </span><br><span class="line">        CardView cardView;</span><br><span class="line">        <span class="comment">//ImageView cardHead;</span></span><br><span class="line">        TextView cardTitle;</span><br><span class="line">        TextView cardSummary;</span><br><span class="line">        </span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="title">ViewHolder</span><span class="params">(View view)</span> </span>&#123;</span><br><span class="line">            <span class="comment">//引入外界的item，在卡片中开始实例化</span></span><br><span class="line">            <span class="keyword">super</span>(view);</span><br><span class="line">            </span><br><span class="line">            cardView = (CardView) view;</span><br><span class="line">            <span class="comment">//cardHead = (ImageView)view.findViewById(R.id.cardHead);</span></span><br><span class="line">            cardTitle = (TextView) view.findViewById(R.id.cardTitle);</span><br><span class="line">            cardSummary = (TextView) view.findViewById(R.id.cardSummary);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">//开始给白卡绑数据</span></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onBindViewHolder</span><span class="params">(ViewHolder whiteCard, <span class="keyword">int</span> positon)</span> </span>&#123;</span><br><span class="line">        Card card = cardList.get(positon); <span class="comment">//通过位置参数判断用户点的是哪个卡片</span></span><br><span class="line">        </span><br><span class="line">        <span class="comment">//whiteCard.cardHead.set...???  //头像图片还没做，这里先记个书签，以后再说</span></span><br><span class="line">        whiteCard.cardTitle.setText(card.getCardTitle());</span><br><span class="line">        whiteCard.cardSummary.setText(card.getCardSummary());</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">//获取列表中卡片总数量</span></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">getItemCount</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> cardList.size();</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h1 id="6-列表的加载"><a href="#6-列表的加载" class="headerlink" title="6. 列表的加载"></a>6. 列表的加载</h1><p>列表的加载在MainActivity中进行，为了让思路更清晰，这里只在MainActivity中引用加载的方法，而具体的代码则新建一个PageRender.java文件放置。</p>
<h2 id="6-1-首先在MainActivity-java中的onCreate-里面新增一行代码"><a href="#6-1-首先在MainActivity-java中的onCreate-里面新增一行代码" class="headerlink" title="6.1. 首先在MainActivity.java中的onCreate()里面新增一行代码"></a>6.1. 首先在MainActivity.java中的onCreate()里面新增一行代码</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MainActivity</span> <span class="keyword">extends</span> <span class="title">AppCompatActivity</span> <span class="keyword">implements</span> <span class="title">View</span>.<span class="title">OnClickListener</span> </span>&#123;</span><br><span class="line">    ...</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">onCreate</span><span class="params">(Bundle savedInstanceState)</span> </span>&#123;</span><br><span class="line">        ...</span><br><span class="line">        <span class="comment">//为左分页加载卡片列表</span></span><br><span class="line">        <span class="keyword">new</span> PageRender(view1); <span class="comment">//新建一个PageRender.java写相关代码</span></span><br><span class="line">    &#125;</span><br><span class="line">    ...</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>注意我们传入了一个<code>view1</code>参数，它是由<code>View view1 = inflater.inflate(R.layout.page_item_all, null);</code>得来的，也就是左分页的布局（我们在开头也提到过，该布局只有一个RecyclerView，背景是浅灰色的）。</p>
<h2 id="6-2-然后新建PageRender-java处理列表加载业务"><a href="#6-2-然后新建PageRender-java处理列表加载业务" class="headerlink" title="6.2. 然后新建PageRender.java处理列表加载业务"></a>6.2. 然后新建PageRender.java处理列表加载业务</h2><p>这是本章的最后一个重点。代码如下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">PageRender</span> <span class="keyword">extends</span> <span class="title">MainActivity</span> </span>&#123;</span><br><span class="line">    <span class="comment">//注意继承自MainActivity</span></span><br><span class="line">    </span><br><span class="line">    <span class="keyword">private</span> List&lt;Card&gt; cardList = <span class="keyword">new</span> ArrayList&lt;Card&gt;();</span><br><span class="line">    <span class="keyword">private</span> CardAdapter cardAdapter;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">PageRender</span><span class="params">(View page)</span> </span>&#123;</span><br><span class="line">        </span><br><span class="line">        <span class="comment">//首先绑定RecyclerView</span></span><br><span class="line">        RecyclerView recyclerView = (RecyclerView) page.findViewById(R.id.recyclerView);</span><br><span class="line">        <span class="comment">/* 关于findViewById是一个重点</span></span><br><span class="line"><span class="comment">         特别注意，必须写成page.findViewById，不能把前缀page省略掉。</span></span><br><span class="line"><span class="comment">         如果省略掉，虽然编译时不会报错，但运行时程序会崩溃。</span></span><br><span class="line"><span class="comment">         通过观察recyclerView的值会发现，崩溃时其值为0，也就是根本不存在这个对象。这是为什么呢？</span></span><br><span class="line"><span class="comment">         原因在于findViewById其实是“view.findViewById”的缩写，这个默认的view是什么呢？就是你当前所在的布局，也就是layout_main.xml，显然我们在layout_main.xml里是找不到RecyclerView控件的，因为RecyclerView在page_item_all.xml布局里</span></span><br><span class="line"><span class="comment">         参考：http://blog.sina.cn/dpool/blog/s/blog_95bc05000101bhxe.html</span></span><br><span class="line"><span class="comment">         */</span></span><br><span class="line">        </span><br><span class="line">        <span class="comment">//初始化数据，将原数据按card为单位切分，并装载到数组里</span></span><br><span class="line">        initCards();</span><br><span class="line">        </span><br><span class="line">        cardAdapter = <span class="keyword">new</span> CardAdapter(cardList); <span class="comment">//根据数据组建出一个个写好的卡片</span></span><br><span class="line">        LinearLayoutManager linearLayoutManager = <span class="keyword">new</span> LinearLayoutManager(<span class="keyword">this</span>);</span><br><span class="line">        recyclerView.setLayoutManager(linearLayoutManager); <span class="comment">//在recyclerView里面放置一个线性布局，以使卡片呈线性排列</span></span><br><span class="line">        recyclerView.setAdapter(cardAdapter); <span class="comment">//将卡片们装载到RecyclerView的布局中</span></span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">//先自建一组数据来测试一下……</span></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">initCards</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        cardList.clear();</span><br><span class="line">        </span><br><span class="line">        <span class="comment">//遍历装载</span></span><br><span class="line">        cardList.add(<span class="keyword">new</span> Card(<span class="string">"Title1"</span>, <span class="string">"name:aaa\npassword:aaa\nnote:aaa"</span>));</span><br><span class="line">        cardList.add(<span class="keyword">new</span> Card(<span class="string">"Title2"</span>, <span class="string">"name:bbb\npassword:bbb\nnote:bbb"</span>));</span><br><span class="line">        cardList.add(<span class="keyword">new</span> Card(<span class="string">"Title3"</span>, <span class="string">"name:ccc\npassword:ccc\nnote:ccc"</span>));</span><br><span class="line">        cardList.add(<span class="keyword">new</span> Card(<span class="string">"Title4"</span>, <span class="string">"name:ddd\npassword:ddd\nnote:ddd\nextra:ddd"</span>)); <span class="comment">//验证是否只显示3行</span></span><br><span class="line">        cardList.add(<span class="keyword">new</span> Card(<span class="string">"Title5"</span>, <span class="string">"name:eee\npassword:eee\nnote:eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"</span>)); <span class="comment">//验证多余字符是否显示为省略号</span></span><br><span class="line">        cardList.add(<span class="keyword">new</span> Card(<span class="string">"Title6"</span>, <span class="string">"name:fff"</span>)); <span class="comment">//验证单行字符是否可以占据3行的空间</span></span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>至此左分页布局代码全部完成，不过很多功能（包括基本功能）都还没有实现，随着后续的学习我们还会不断完善这个布局，直到应用到实战中去。</p>
<p>下章会写右分页布局，然后很快就能联系左分页的知识模拟完整的数据加载逻辑了。</p>
<hr>
<h1 id="相关参考"><a href="#相关参考" class="headerlink" title="相关参考"></a>相关参考</h1><ol>
<li>卡片式布局：郭霖《第一行代码 第二版》p431</li>
<li>RecyclerView：郭霖《第一行代码 第二版》p122</li>
<li><a href="http://blog.csdn.net/qq_31403303/article/details/51506524" target="_blank" rel="noopener">http://blog.csdn.net/qq_31403303/article/details/51506524</a></li>
<li><strong>findViewById <a href="http://blog.sina.cn/dpool/blog/s/blog_95bc05000101bhxe.html" target="_blank" rel="noopener">http://blog.sina.cn/dpool/blog/s/blog_95bc05000101bhxe.html</a></strong></li>
</ol>
<hr>
<h1 id="日志"><a href="#日志" class="headerlink" title="日志"></a>日志</h1><p><strong>2017年11月21日</strong></p>
<ol>
<li>【新增】左分页制作列表界面</li>
</ol>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
        <div class="post-widgets">
        

        

        
          
          <div id="needsharebutton-postbottom">
            <span class="btn">
              <i class="fa fa-share-alt" aria-hidden="true"></i>
            </span>
          </div>
        
        </div>
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/1123203012/" rel="next" title="02 Anykey左右滑动布局ViewPager">
                <i class="fa fa-chevron-left"></i> 02 Anykey左右滑动布局ViewPager
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017/1125123725/" rel="prev" title="EditText密码输入错误后重新获取焦点并自动弹出软键盘.md">
                EditText密码输入错误后重新获取焦点并自动弹出软键盘.md <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
      <div id="lv-container" data-id="city" data-uid="MTAyMC8zNDI4MS8xMDgxOA=="></div>
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="http://ozurciydg.bkt.clouddn.com/18-2-12/72826549.jpg"
                alt="Likianta" />
            
              <p class="site-author-name" itemprop="name">Likianta</p>
              <p class="site-description motion-element" itemprop="description">Android Dev</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">73</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">8</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">2</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          
            <div class="feed-link motion-element">
              <a href="/atom.xml" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
            </div>
          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="http://blog.csdn.net/likianta" target="_blank" title="CSDN">
                      
                        <i class="fa fa-fw fa-crosshairs"></i>CSDN</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="mailto:sheerish@qq.com" target="_blank" title="E-Mail">
                      
                        <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/likianta" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://weibo.com/5441767757/profile?rightmod=1&wvr=6&mod=personinfo" target="_blank" title="Weibo">
                      
                        <i class="fa fa-fw fa-weibo"></i>Weibo</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#1-首先看一下最终的效果图"><span class="nav-text">1. 首先看一下最终的效果图</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#2-配置RecyclerView和CardView"><span class="nav-text">2. 配置RecyclerView和CardView</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#3-制作RecyclerView布局"><span class="nav-text">3. 制作RecyclerView布局</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#4-新建card-xml制作卡片模板"><span class="nav-text">4. 新建card.xml制作卡片模板</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#5-卡片的属性及适配"><span class="nav-text">5. 卡片的属性及适配</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#5-1-首先新建Card-java，制作卡牌模型"><span class="nav-text">5.1. 首先新建Card.java，制作卡牌模型</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-2-然后制作卡片适配器CardAdapter"><span class="nav-text">5.2. 然后制作卡片适配器CardAdapter</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#6-列表的加载"><span class="nav-text">6. 列表的加载</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#6-1-首先在MainActivity-java中的onCreate-里面新增一行代码"><span class="nav-text">6.1. 首先在MainActivity.java中的onCreate()里面新增一行代码</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#6-2-然后新建PageRender-java处理列表加载业务"><span class="nav-text">6.2. 然后新建PageRender.java处理列表加载业务</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#相关参考"><span class="nav-text">相关参考</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#日志"><span class="nav-text">日志</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>
<div class="copyright">&copy; 2017 &mdash; <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Likianta</span>

  
</div>


  <div class="powered-by">
    <i class="fa fa-user-md"></i>
    <span id="busuanzi_container_site_pv">
      Site visitors:<span id="busuanzi_value_site_pv"></span>
    </span>
  </div>

<!--
  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Mist</a> v5.1.3</div>

-->



<div class="theme-info">
  <div class="powered-by"></div>
  <span class="post-count">Site words total count:75.2k</span>
</div>
        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.3"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.3"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.3"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.3"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.3"></script>



  


  




	





  





  
    <script type="text/javascript">
      (function(d, s) {
        var j, e = d.getElementsByTagName(s)[0];
        if (typeof LivereTower === 'function') { return; }
        j = d.createElement(s);
        j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
        j.async = true;
        e.parentNode.insertBefore(j, e);
      })(document, 'script');
    </script>
  












  





  

  

  

  
  
  
  <link rel="stylesheet" href="/lib/needsharebutton/needsharebutton.css">

  
  
  <script src="/lib/needsharebutton/needsharebutton.js"></script>

  <script>
    
      pbOptions = {};
      
          pbOptions.iconStyle = "box";
      
          pbOptions.boxForm = "horizontal";
      
          pbOptions.position = "bottomCenter";
      
          pbOptions.networks = "Weibo,Wechat,Douban,QQZone,Evernote";
      
      new needShareButton('#needsharebutton-postbottom', pbOptions);
    
    
  </script>

  

  

  

  

</body>
</html>
